using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace UKVSTS.WorkItemControls
{
    public partial class WorkItemCheckBox : BaseWorkItemControl
    {
        private const String CHECKED_INDEX = "Checked";

        private const String UNCHECKED_INDEX = "Unchecked";

        private const String TEXT_INDEX = "Text";

        public WorkItemCheckBox ()
        {
            InitializeComponent ();

            checkBox.CheckedChanged += new EventHandler (checkBox_CheckedChanged);

        }

        protected override void OnPropertiesSet ()
        {
            String val = Properties[TEXT_INDEX];
            if (String.IsNullOrEmpty (val) == false)
            {
                checkBox.Text = val;
            }
        }

        void checkBox_CheckedChanged (object sender, EventArgs e)
        {
            String index = checkBox.Checked ? CHECKED_INDEX : UNCHECKED_INDEX;
            SetFieldValue (Properties[index]);
        }

        private void ConfirmAllowedValueForField (String propIndex)
        {
            String val = Properties[propIndex];

            if (val == null)
            {
                SetControlInError (String.Format (
                    "Could not find values for {0} state. Please enuse that the {1} and {2} attributes are defined in the Work Item Type definition.",
                    propIndex,
                    CHECKED_INDEX,
                    UNCHECKED_INDEX));
            }
            else
            {
                if (Field.IsLimitedToAllowedValues)
                {
                    if (Field.FieldDefinition.AllowedValues.Contains (val) == false)
                    {
                        SetControlInError (String.Format (
                            "The value specified in the Work Item Definition for the {0} attribute is not one of the allowed values for the {1} Field",
                            propIndex,
                            WorkItemFieldName));
                    }
                }
            }
        }

        public override void Clear ()
        {
            checkBox.Checked = false;
        }

        public override void OnDataSourceChanged ()
        {
            ConfirmAllowedValueForField (CHECKED_INDEX);
            ConfirmAllowedValueForField (UNCHECKED_INDEX);

            if (!InError)
            {
                bool check = (Field.Value.ToString () == Properties[CHECKED_INDEX]);
                checkBox.Checked = check;
            }
        }

        protected override void ResetStyle ()
        {
            checkBox.Enabled = (ReadOnly == false);
            if (InError)
            {
                BackColor = Color.Red;
            }
            else
            {
                BackColor = SystemColors.ButtonFace;
            }

        }
    }
}